import React, { Component } from 'react';
import { StyleSheet, Text, View, Dimensions } from "react-native";
import { style } from '../../../../../theme/style';

import { GoodsRowItem } from './GoodsRowItem';
import { GoodsSelectCpt } from './GoodsSelectCpt';
import { showMap, mandatory } from './offerMap';
import { SelectCpt } from './SelectCpt';
import { title } from './/offerMap';
import { Methods } from '../../_common/Common';
import Utils from "../../../../../utils/utils";
import ToastUtil from "../../../../../utils/toast";

const ScreenWidth = Dimensions.get('window').width;
const scale = ScreenWidth / 375;

export class GoodsItem extends Component {

    constructor(props) {
        super(props);
        let { data } = props || {};

        this.state = {

        }
    }

    // componentWillReceiveProps (props) {
    //     console.log('商品监听 ------------------', props);
    // }


    // 输入
    _writeFn = (writeData) => {
        console.log('writeData', writeData);
        let { data, event } = this.props || {};
        let { goodsIndex, goodsData } = data || {};
        let { changeGoods } = event || {};

        goodsData[goodsIndex] = Object.assign({}, goodsData[goodsIndex], writeData);
        // 如果触发了 价格类型修改
        if (writeData.target) {
            goodsData = Methods.clearGoodsPriceType(goodsData, writeData, goodsIndex);
        }
        changeGoods && changeGoods(goodsData);
        if (writeData.priceType) {
            this.setState({
                ls: true
            })
        }
    }

    //焦点离开数据
    _onBlurFn = (id) => {
        let { data } = this.props || {};
        let { goodsIndex, goodsData } = data || {};
        let { offerMultiple, minOfferQuantity } = goodsData[goodsIndex];
        //设置倍量，最小起报值为必输
        if ((id === "offerMultiple" || id === "minOfferQuantity") && !Utils.isEmpty(offerMultiple) && Utils.isEmpty(minOfferQuantity)) {
            ToastUtil.showWarning(`设置倍量，最小起报值为必输`, 3000);
        }
    }

    //
    removeGoods = () => {
        let { event } = this.props || {};
        let { removeGoods } = event || {};
        removeGoods && removeGoods();
    }

    //商品改变
    _changeGoods = (goodsData, goodsIndex) => {
        let { event } = this.props || {};
        let { changeGoods } = event || {};
        if (goodsData[goodsIndex]) {
            goodsData[goodsIndex].priceFormlaId = null;
            goodsData[goodsIndex].priceFormlaName = null;
        }
        changeGoods && changeGoods(goodsData);
    }

    render() {
        let { data, event, dictionaryData, notAdd } = this.props || {};
        let { bizModel, goodsIndex, goodsData, salesType, salesRuleId } = data || {};
        let { removeGoods, changeGoods } = event || {};
        let goodsDataObj = goodsData[goodsIndex] || {};
        let { priceType, maxCanSellNum, priceFormlaId, udPrice, symbol, SettlementData,
            minBaseValue, maxBaseValue, offerBottomPrice, minOfferQuantity, offerMultiple,
            custPurNum, stepPrice, price, priceFormlaName, goodsId } = goodsDataObj || {};
        return (
            <View style={styles.goodsBox}>
                <GoodsSelectCpt
                    notAdd={notAdd}
                    data={{ require: true, type: 'select', removeText: '删', goodsIndex, goodsData, salesRuleId }}
                    event={{ removeGoods: removeGoods, changeGoods: this._changeGoods }} />
                <GoodsRowItem
                    notAdd={notAdd}
                    data={{ require: true, id: 'maxCanSellNum', type: 'input', title: title.plan[salesType], unit: '吨', placeholder: '0.00', defaultValue: maxCanSellNum }}
                    event={{ onInputChange: this._writeFn }}
                />
                {showMap.isBaseValueFn(bizModel) ? <GoodsRowItem notAdd={notAdd} data={{ require: true, type: 'Scope', title: '成交量范围', keyStart: 'minBaseValue', keyEnd: 'maxBaseValue', minBaseValue, maxBaseValue }} event={{ onInputChange: this._writeFn }} /> : null}
                <GoodsRowItem notAdd={notAdd} key={new Date()} dictionaryData={dictionaryData} allGoodsData={{ goodsIndex: goodsIndex, goodsData: goodsData }} data={{ require: true, type: 'priceType', title: '价格类型', priceType: priceType }} event={{ onInputChange: this._writeFn }} />
                {
                    priceType == '2' ? <SelectCpt
                        data={{
                            id: 'priceFormlaId',
                            title: '结算公式',
                            listData: SettlementData || [],
                            priceFormlaName: priceFormlaName,
                            priceFormlaId: priceFormlaId,
                            required: true,
                            orderType: salesType,
                            goodsId: goodsId
                        }}
                        style={{
                            keyName: { width: 60 * scale, marginLeft: 20 * scale },
                            valueTextStyle: {
                                width: 230 * scale, textAlign: 'right', fontSize: 12 * scale,
                                color: '#000'
                            }
                        }}
                        callBack={this._writeFn}
                    /> : null
                }
                {
                    priceType == '2' ? <GoodsRowItem data={{ bizModel, id: 'udPrice', type: 'Stick', placeholder: '0.00', unit: '元/吨', defaultValue: udPrice, symbol: symbol }} event={{ onInputChange: this._writeFn }} /> : null
                }
                {
                    priceType == '2' ? <GoodsRowItem
                        data={{ id: 'price', defaultValue: price, type: 'input', title: '暂定价', unit: '元/吨', placeholder: '0.00' }}
                        event={{ onInputChange: this._writeFn }}
                        style={{ inputStyle: { width: 110 * scale } }}
                    /> : null
                }
                {
                    showMap.isPriceFn(bizModel) && priceType == '1' ? <GoodsRowItem data={{ require: true, id: 'price', defaultValue: price, type: 'input', title: '含税单价', unit: '元/吨', placeholder: '0.0' }} event={{ onInputChange: this._writeFn }} style={{ inputStyle: { width: 110 * scale } }} /> : null
                }
                {
                    showMap.isOfferBottomPriceFn(bizModel) && priceType == '1' ?
                        <GoodsRowItem
                            notAdd={notAdd}
                            data={{ require: mandatory.isOfferBottomPriceFn(bizModel), id: 'offerBottomPrice', defaultValue: offerBottomPrice, type: 'input', title: '报盘底价', unit: '元/吨', placeholder: '0.0' }}
                            style={{ inputStyle: { width: 110 * scale } }} event={{ onInputChange: this._writeFn }} />
                        : null
                }
                <GoodsRowItem
                    data={{ id: 'minOfferQuantity', defaultValue: minOfferQuantity, type: 'input', title: '最小报盘数量', unit: '吨', placeholder: '0.00' }}
                    event={{ onInputChange: this._writeFn, onInputOnBlur: this._onBlurFn }}
                />
                <GoodsRowItem
                    data={{ id: 'offerMultiple', defaultValue: offerMultiple, type: 'input', title: '报盘倍量', unit: '吨', placeholder: '请输入大于0的整数' }}
                    event={{ onInputChange: this._writeFn, onInputOnBlur: this._onBlurFn }}
                />
                {
                    showMap.isCustPurNumFn(bizModel) ? <GoodsRowItem notAdd={notAdd} data={{ id: 'custPurNum', defaultValue: custPurNum, type: 'input', title: title.number[salesType], unit: '吨', placeholder: '0.00' }} event={{ onInputChange: this._writeFn }} /> : null
                }
                {
                    salesType === "S" && showMap.isCustPurNumFn(bizModel) ? <View style={styles.tipBox}><Text style={styles.tipIcon}>*</Text><Text style={styles.tip}>设定客户采购量，客户将以此量提报不可修改</Text></View> : null
                }
                {
                    showMap.isStepPriceFn(bizModel) ? <GoodsRowItem style={{ inputStyle: { width: 100 * scale } }} notAdd={notAdd} data={{ id: 'stepPrice', defaultValue: stepPrice, type: 'input', title: '客户提报含税单价加价梯度', unit: '元/吨', placeholder: '0.000' }} event={{ onInputChange: this._writeFn }} /> : null
                }
            </View>
        )
    }
}

const styles = StyleSheet.create({
    goodsBox: {
        backgroundColor: style.color.white,
        borderRadius: 5 * scale,
        marginBottom: 25 * scale
    },
    tipBox: {
        paddingVertical: 10 * scale,
        paddingHorizontal: 10 * scale,
        marginBottom: 5 * scale,
        flexDirection: 'row',
        justifyContent: 'flex-end',
        alignItems: 'center',
    },
    tip: {
        color: 'red',
        fontSize: 12 * scale,
    },
    tipIcon: {
        color: 'red',
        fontSize: 16 * scale,
        marginRight: 3 * scale,
        marginTop: 3 * scale
    }
})